﻿using IBLL;
using IDAL;
using MODEL;
using System.Collections.Generic;

namespace BLL
{
    public class OrganizationBLL : IOrganizationBLL
    {
        public OrganizationBLL(IBaseDAL<Organization> dal,IBaseDAL<Service> dals)
        {
            Dal = dal;
            Dals = dals;
        }

        public IBaseDAL<Organization> Dal { get; }
        public IBaseDAL<Service> Dals { get; }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="organization">机构信息对象</param>
        /// <returns>受影响行数</returns>
        public async Task<int> AddOrgan(Organization organization)
        {
            return await Dal.AddAsync(organization);
        }
        /// <summary>
        /// 查询+分页
        /// </summary>
        /// <param name="name">机构名称</param>
        /// <param name="sid">服务类型</param>
        /// <param name="pageindex">页码</param>
        /// <param name="pagesize">页容量</param>
        /// <returns>返回匿名类</returns>
        public dynamic GetList(string? name, int sid, int pageindex, int pagesize)
        {
            var list = Dal.GetValues().Where(x => x.IsDel == false);
            var lists = Dals.GetValues();
            if (!string.IsNullOrEmpty(name)) 
            {
                list = list.Where(x => x.Oname.Contains(name));
            }
            if (sid > 0) 
            {
                list=list.Where(x => x.Sid == sid); 
            }
            var query = from a in list
                        join b in lists
                        on a.Sid equals b.Sid
                        select new
                        {
                            Oid = a.Oid,
                            Oname = a.Oname,
                            Oimg = a.Oimg,
                            Sid = a.Sid,
                            StrarServiceTime = a.StrarServiceTime,
                            Address = a.Address,
                            Phone = a.Phone,
                            IsPublish = a.IsPublish,
                            Onote = a.Onote,
                            IsDel = a.IsDel,
                            Sname = b.Sname,
                        };
            var s = query.Count();
            var d=query.OrderBy(x=>x.Oid).Skip((pageindex-1)*pagesize).Take(pagesize).ToList();
            return new
            {
                count = s,
                row = d,
            };
        }
        /// <summary>
        /// 获取导出的列表集合
        /// </summary>
        /// <returns></returns>
        public List<Organization> GetOrga()
        {
            return Dal.GetValues().ToList();
        }

        /// <summary>
        /// 数据绑定服务
        /// </summary>
        /// <returns>返回服务表集合</returns>
        public List<Service> GetServices()
        {
            return Dals.GetValues().ToList();
        }
        /// <summary>
        /// 批量删除（逻辑）
        /// </summary>
        /// <param name="idstring">字符串数组</param>
        /// <returns>受影响行数</returns>
        public async Task<int> RangeDelete(string idstring)
        {
            List<int> list=idstring.Split(',').Select(x=>Convert.ToInt32(x)).ToList();
            List<Organization> Arr=Dal.GetValues().Where(x=>list.Contains(x.Oid)).ToList();
            foreach (Organization org in Arr) 
            {
                org.IsDel = true;
            }
            return await Dal.UpdateRangeAsync(Arr);
        }
        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="idstring">字符串数组</param>
        /// <returns>受影响行数</returns>
        public async Task<int> UpdateStutus(string idstring)
        {
            List<int> list = idstring.Split(',').Select(x => Convert.ToInt32(x)).ToList();
            List<Organization> Arr = Dal.GetValues().Where(x => list.Contains(x.Oid)).ToList();
            foreach (Organization org in Arr)
            {
                org.IsPublish = !org.IsPublish;
            }
            return await Dal.UpdateRangeAsync(Arr);
        }
    }
}
